package com.ir.stat.spark.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.ir.stat.spark.rest.StatAction;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Configuration()
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {

    @PostConstruct
    protected void configAfterConstruct() {
        super.register(AuthTokenFilter.class);
        super.register(ObjectMapperContextResolver.class);
        super.register(StatAction.class);
    }


}

@Provider
class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    static boolean prettyJson = false;

    private ObjectMapper mapper;

    public ObjectMapperContextResolver() {
        mapper = new ObjectMapper();
        if (prettyJson) {
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
        }
        mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    public ObjectMapper getContext(Class clz) {
        return mapper;
    }

}